import axios from 'axios'
export const baseUrl = 'http://116.62.185.51:2202'

import store from '@/store'
const { userInfo } = store.getState()

const request = axios.create({
    baseURL: baseUrl + '/api',

    // bug:直接写在axios.create中的请求头数据无法实现更新
    // 解决方案：axios请求拦截
    headers: {
        Authorization: userInfo.authorization
    },
})

// 请求拦截：请求时才获取数据的方式
request.interceptors.request.use((config) => {
    // 给增删改添加Authorization请求头

    if (['post', 'delete', 'put', 'patch', 'get'].includes(config.method)) {

        const { userInfo } = store.getState()
        config.headers.Authorization = userInfo.authorization

    }
    return config;
})


export default request
